/*

filipesilvestrim - An open-source ActionScript 3.0 repository of codes for Game Developers 

http://code.google.com/p/filipesilvestrim/

Copyright (c) 2009 Filipe Ghesla Silvestrim, All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/

package com.filipesilvestrim.util
{
	import flash.display.DisplayObject;
	import flash.geom.Rectangle;
	import flash.net.registerClassAlias;
	import flash.utils.ByteArray;
	
	public class Copier
	{
		static private var _cla : Class;
		
		static public function registerClass ( classPath : String, cla : Class) : void
		{
			_cla = cla;
			registerClassAlias(classPath, _cla);
		}
		
		/**
		 * Please do cast as Class(bytes.readObject() as Class)
		 */
		static public function deepClone ( obj : * ) : *
		{
			var bytes : ByteArray = new ByteArray();
			bytes.writeObject(obj);
			bytes.position = 0;
			return bytes.readObject();
		}
		
		/**
	     * duplicateDisplayObject
	     * creates a duplicate of the DisplayObject passed.
	     * similar to duplicateMovieClip in AVM1
	     * @param target the display object to duplicate
	     * @param autoAdd if true, adds the duplicate to the display list
	     * in which target was located
	     * @return a duplicate instance of target
	     */
	    static public function duplicateDisplayObject(target:DisplayObject, autoAdd:Boolean = false):DisplayObject 
	    {
	        // create duplicate
	        var targetClass	: Class 			= Object(target).constructor;
	        var duplicate	: DisplayObject 	= new targetClass();
	        
	        // duplicate properties
	        duplicate.transform 			= target.transform;
	        duplicate.filters 				= target.filters;
	        duplicate.cacheAsBitmap 		= target.cacheAsBitmap;
	        duplicate.opaqueBackground 		= target.opaqueBackground;
	        
	        if (target.scale9Grid) 
	        {
	            var rect:Rectangle = target.scale9Grid;
	            // WAS Flash 9 bug where returned scale9Grid is 20x larger than assigned
	            // rect.x /= 20, rect.y /= 20, rect.width /= 20, rect.height /= 20;
	            duplicate.scale9Grid = rect;
	        }
	        
	        if (autoAdd && target.parent) 
	            target.parent.addChild(duplicate);
	        
	        return duplicate;
	    }
	}
}